#!/bin/bash
BASE_DIR=`cd $(dirname $0);pwd`
#set -x
echo_message(){
        echo  "------------> $1"
}

fouction_stop_firewalld(){
	echo_message "检查防火墙是否开启......"
	systemctl status firewalld |grep "running"
	if [ "$?" -ne '0'  ];then
		echo_message "防火墙未开启，不做任何操作"
	else
		echo_message "防火墙已开启，执行关闭操作"
		systemctl disable --now firewalld && \
		echo_message "防火墙关闭成功"
	fi
}

fouction_stop_selinux(){
	echo_message "检查selinux是否开启......"	
	STATE=`getenforce`
	if [ "${STATE}" == "Disabled" ];then
		FILE_STATE=`grep "^SELINUX=" /etc/selinux/config`
		if [ "${FILE_STATE}" == 'SELINUX=disabled' ];then
			echo_message "selinux未开启，不做任何操作"
		else
			echo_message "/etc/selinux/config 文件中selinux为enabled，执行修改为disabled操作"
			sed -i '/^SELINUX=/s#enabled#disabled#g' /etc/selinux/config && \
			echo_message "修改完成"
		fi
	else
		echo_message "selinux已经开启，执行关闭操作"
		setenforce 0 
		sed -i '/^SELINUX=/s#enabled#disabled#g' /etc/selinux/config && \
		echo_message "关闭完成"
	fi
	
}

fouction_ssh(){

	fouction_ip_file(){
                read -p "请输入名为ip的文件所在目录。如果没有，请将需要免密登录的ip地址和密码写入名为ip的文件中使用'|'隔开，一个ip占用一行:" IP_FILE_DIR
                if [ -f "${IP_FILE_DIR}/ip" ];then
	        	echo_message "检查${IP_FILE_DIR}/ip文件成功"
	        else
	        	echo_message "检查${IP_FILE_DIR}/ip文件不存在，请重新输入" && fouction_ip_file
                fi
	}
	fouction_user(){
		read -p "请输入需要免密登录的用户名:" USER
        	if [ ! -z "${USER}" ];then
        	        echo_message "检查用户名输入成功"
		else
			echo_message "检查用户名输入失败，请重新输入" && fouction_user
        	fi
        }

	fouction_ip_file
        fouction_user

        if [ -f ~/.ssh/id_rsa.pub ];then
                echo_message "公钥已经存在，不用另外生成" 
        else
                echo_message "公钥不存在，生成公钥" 
                ssh-keygen -t rsa -P "" -f ~/.ssh/id_rsa
        fi

        which sshpass &>/dev/null
        if [ "$?" -eq '0' ];then
                echo_message "sshpass 已经安装" 
        else
                echo_message "sshpass 未经安装，尝试使用yum或者apt安装sshpass" 
                yum -y install sshpass openssh-clients
		apt -y install sshpass

                which sshpass &>/dev/null
                if [ "$?" -eq '0' ];then
                        echo_message "sshpass安装完成"
                else
                        echo_message "sshpass安装失败，请手动安装" && exit 404
                fi

        fi

        for i in `cat ${IP_FILE_DIR}/ip`;do
                PASSWORD=`echo ${i}|awk -F '|' '{print $2}'`
                DEST_IP=`echo ${i}|awk  -F '|' '{print $1}'`
                sshpass -p ${PASSWORD} ssh-copy-id -i ~/.ssh/id_rsa.pub -o "StrictHostKeyChecking"=no ${USER}@${DEST_IP} &>/dev/null
                if [ $? -eq 0 ];then
                        HOSTNAME=`ssh ${USER}@${DEST_IP} "hostname -I" |awk '{print $1}'`
                        if [ ${HOSTNAME} == ${DEST_IP} ];then
                                echo_message "${DEST_IP} 免密登陆完成" 
                        else
                                echo_message "${DEST_IP} 免密登陆失败"
                        fi
                else
                        echo_message "${DEST_IP} 发送公共钥匙失败"
                fi
        done
}

fouction_install_JDK (){
        read -p "请输入安装路径,并且将JDK压缩包上传至脚本所在路经:" JDK_INSTALL_DIR

        JDK_PKG_PATH=${BASE_DIR}/
        JDK_PKG_NAME=`ls ${JDK_PKG_PATH} |egrep  "jdk-[0-9]u[0-9]{1,3}"`
        JDK_ENV_FILE=/etc/profile

	if [ ! -z "${JDK_INSTALL_DIR}" ] && [ -f "${JDK_PKG_PATH}/${JDK_PKG_NAME}" ];then
		echo_message "检查输入的安装路经成功；检查上传的JDK包存在"
	else
		echo_message "检查输入的安装路经失败，或者检查上传的JDK包不存在。请重新确认安装路经以及上传JDK压缩包后再次启动脚本。" && \
		#fouction_install_JDK
		exit 404
	fi


        #检查系统是不是装了JDK
        JDK_EXIST=`which java &> /dev/null ; echo $?`
        if [ ${JDK_EXIST} -eq '0' ] ;then
                echo_message "JDK 已经存在" && exit 404
        else
                cd ${JDK_PKG_PATH} && tar -xf ${JDK_PKG_NAME} -C ${JDK_INSTALL_DIR}

                JDK_DIR_NAME=`ls ${JDK_INSTALL_DIR} |grep "jdk[1-9]\.[0-9]\.[0-9].*"`
                if [ ! -z ${JDK_DIR_NAME} ];then
                        echo "#JDK" >>${JDK_ENV_FILE}
                        echo "export JAVA_HOME=${JDK_INSTALL_DIR}/${JDK_DIR_NAME}" >>${JDK_ENV_FILE}
                        echo 'export CLASSPATH=${JAVA_HOME}/lib/' >>${JDK_ENV_FILE}
                        echo 'export PATH=${PATH}:${JAVA_HOME}/bin' >>${JDK_ENV_FILE}

			echo -e "------------>\033[32m JDK安装完成，请退出脚本后复制此命令并执行：source ${JDK_ENV_FILE} \033[0m"
                else
                        echo_message "JDK 解压失败" && exit 404
                fi


        fi
}

fouction_sha256sum(){
	read -p "请输入要使用sha256sum加密的字符串：" STRING
	if [ ! -z "${STRING}" ];then
		echo_message "加密结果如下："
		echo -n "${STRING}" |sha256sum |tr -d -
	else
		echo_message "检查到输入的字符串为空，请重新执行脚本输入"
	fi
}

fouction_password(){
	read -p "请输入生成的密码位数：" NUMBER
	if [ ! -z "${NUMBER}" ];then
        	echo_message "密码如下："
		PASSWORD=$(base64 < /dev/urandom | head -c`expr ${NUMBER} - 2`);echo "$PASSWORD!5"
        else
                echo_message "检查到输入的字符串为空，请重新执行脚本输入"
        fi
}

fouction_lvm(){

fouction_VGcreate(){
        echo_message "磁盘信息如下："
        lsblk
        echo_message "逻辑卷的卷组情况如下："
        vgs
        echo_message "设备挂载情况如下："
        df -Th |grep -v "containerd" |grep -v "docker" |grep -v "kubelet"
        echo

        read -p "VGcreate,请输入磁盘名和卷组名,使用空格隔开:" DISK_NAME VG_NAME
        if [ ! -z "${DISK_NAME}" ] && [ ! -z "${VG_NAME}" ];then
                pvcreate ${DISK_NAME} 
                if [ "$?" -eq '0' ];then
                        echo_message "pvcreate执行成功,PV情况入下" && pvs
                        vgcreate ${VG_NAME} ${DISK_NAME}
                        if [ "$?" -eq '0' ];then
                                echo_message "vgcreate执行成功,逻辑卷的卷组情况如下" && vgs
                        else
                                echo_message "vgcreate执行失败,移除根据${DISK_NAME}创建的PV,请重新输入"
                                pvremove ${DISK_NAME} 
                                fouction_VGcreate
                        fi
                else
                        echo_message "pvcreate执行失败,请重新输入" && fouction_VGcreate
                fi

        else
                echo_message "检测到您没有输入磁盘名或者卷组名，请输入磁盘名和卷组名,使用空格隔开:" && fouction_VGcreate
        fi
}

fouction_LVcreate(){
        fouction_read_vars(){
                read -p "LVcreate,请输入逻辑卷名，逻辑卷大小例如500M、40G，使用空格隔开:" LV_NAME LV_SIZE
                LV_SIZE_NUMBER=`echo "${LV_SIZE}" |grep -o "[0-9]*"`
                M=`echo "${LV_SIZE}" |grep -o "M$"`
        }

        fouction_read_vars

        if [ "${LV_SIZE_NUMBER}" -lt '16' ] && [ "${M}" == 'M' ];then
                echo_message "监测到输入的逻辑卷数值过小，请重新输入，大小必须>=16M"
                fouction_read_vars

        fi


        if [ ! -z "${LV_NAME}" ] && [ ! -z "${LV_SIZE}" ];then
                lvcreate -L +${LV_SIZE} -n ${LV_NAME} ${VG_NAME}
                if [ "$?" -eq '0' ];then
                        echo_message "lvcreate执行成功,逻辑卷情况如下" && lvs
                else
                        echo_message "lvcreate执行失败，请重新输入执行" && fouction_LVcreate
                fi
        else
                echo_message "检测到您没有输入逻辑卷名或者辑卷大小，请输入逻辑卷名，逻辑卷大小，使用空格隔开" && fouction_LVcreate
        fi
}

fouction_MakeFileSystem(){
        read -p "请输入要创建的文件系统类型，ext4或者xfs": FILESYSTEM
        if [ ! -z "${FILESYSTEM}" ] && [ "${FILESYSTEM}" == "ext4" ];then
                mkfs.ext4 /dev/mapper/${VG_NAME}-${LV_NAME} 
        elif [ ! -z "${FILESYSTEM}" ] && [ "${FILESYSTEM}" == "xfs" ];then
                mkfs.xfs /dev/mapper/${VG_NAME}-${LV_NAME}
        else
                echo "输入错误，请重新输入" && fouction_MakeFileSystem
        fi
}

foucton_MountFileSystem(){
        read -p "请输入挂载点": MOUNTDEST
        if [ -d "${MOUNTDEST}" ];then
                echo "${MOUNTDEST}已经存在，请重新输入" && foucton_MountFileSystem
        else
                mkdir -p ${MOUNTDEST} && mount /dev/mapper/${VG_NAME}-${LV_NAME} ${MOUNTDEST}
                echo "/dev/mapper/${VG_NAME}-${LV_NAME} ${MOUNTDEST} ${FILESYSTEM} defaults 0 0 " >>/etc/fstab 
                echo_message "逻辑卷挂载信息如下："
                df -Th |grep -v "containerd" |grep -v "docker" |grep -v "kubelet"  && \
                echo_message "开机自动挂载信息如下："
                tail -1 /etc/fstab
        fi
}

fouction_VGextend(){
        echo_message "磁盘信息如下："
        lsblk 
        echo_message "逻辑卷的卷组情况如下："
        vgs 
        echo_message "设备挂载情况如下："
        df -Th |grep -v "containerd" |grep -v "docker" |grep -v "kubelet"
        echo 
        read -p "VGextend,请输入磁盘名和卷组名,使用空格隔开:" DISK_NAME VG_NAME
        if [ ! -z "${DISK_NAME}" ] && [ ! -z "${VG_NAME}" ];then
                pvcreate ${DISK_NAME} 
                if [ "$?" -eq '0' ];then
                        echo_message "pvcreate执行成功,PV情况入下" && pvs

                        vgextend ${VG_NAME} ${DISK_NAME}
                        if [ "$?" -eq '0' ];then
                                echo_message "vgextend执行成功,逻辑卷的卷组情况如下" && vgs
                        else
                                echo_message "vgextend执行失败,移除根据${DISK_NAME}创建的PV,请重新输入"
                                pvremove ${DISK_NAME}
                                fouction_VGextend
                        fi
                else
                        echo_message "pvcreate执行失败,请重新输入" && fouction_VGextend
                fi

        else
                echo "检测到您没有输入磁盘名或者卷组名，请输入磁盘名和卷组名,使用空格隔开:" && fouction_VGcreate
        fi  
}

fouction_LVextend(){

        fouction_read_vars(){
                read -p "LVcreate,请输入逻辑卷名，逻辑卷大小例如500M、40G，使用空格隔开:" LV_NAME LV_SIZE
                LV_SIZE_NUMBER=`echo "${LV_SIZE}" |grep -o "[0-9]*"`
                M=`echo "${LV_SIZE}" |grep -o "M$"`
        }

        fouction_read_vars

        if [ "${LV_SIZE_NUMBER}" -lt '16' ] && [ "${M}" == 'M' ];then
                echo_message "监测到输入的逻辑卷数值过小，请重新输入，大小必须>=16M"
                fouction_read_vars

        fi


        if [ ! -z "${LV_NAME}" ] && [ ! -z "${LV_SIZE}" ];then
                lvextend -L +${LV_SIZE} /dev/mapper/${VG_NAME}-${LV_NAME}
                if [ "$?" -eq '0' ];then
                        echo_message "lvextend执行成功,逻辑卷情况如下" && lvs
                else
                        echo_message "lvextend执行失败，请重新输入执行" && fouction_LVextend
                fi
        else
                echo "检测到您没有输入逻辑卷名或者辑卷大小，请输入逻辑卷名，逻辑卷大小，使用空格隔开" && fouction_LVcreate
        fi  
}

fouction_FlushFileSystem(){
        read -p "FlushFileSystem,请输入文件系统类型ext4或者xfs:" FILESYSTEM
        if [ ! -z "${FILESYSTEM}" ];then
                case ${FILESYSTEM} in
                        xfs)
                        xfs_growfs /dev/mapper/${VG_NAME}-${LV_NAME}
                ;;
                        ext4)
                        resize2fs /dev/mapper/${VG_NAME}-${LV_NAME}
                ;;
                esac

        else
                echo "检测到您没有输入文件系统类型xfs或者ext4，请重新输入" && fouction_FlushFileSystem
        fi
}

read -p "请输入[create|extend]:" SELECT
case ${SELECT} in
        create)
        fouction_VGcreate
        fouction_LVcreate
        fouction_MakeFileSystem
        foucton_MountFileSystem
;;
        extend)
        fouction_VGextend
        fouction_LVextend
        fouction_FlushFileSystem
;;
        *)
        echo "请输入[create|extend]"
esac

}


fouction_exec(){

read -p "######Hello!#######
1 关闭防火墙
2 关闭selinux
3 ssh免密登录
4 安装JDK
5 使用sha256加密
6 生成随机密码
7 LVM逻辑卷创建扩容
请输入想要执行的功能序号。按回车执行，Control+c退出脚本：" NUMBER

case ${NUMBER} in
	1)
	fouction_stop_firewalld  && echo_message "本功能执行结束"
;;
	2)
	fouction_stop_selinux && echo_message "本功能执行结束"
;;
	3)
	fouction_ssh && echo_message "本功能执行结束"
;;
	4)
	fouction_install_JDK && echo_message "本功能执行结束"
;;
	5)
	fouction_sha256sum && echo_message "本功能执行结束"
;;
	6)
	fouction_password && echo_message "本功能执行结束"
;;
	7)
	fouction_lvm && echo_message "本功能执行结束"
;;
	*)
	echo_message "输入错误"
esac 
}
fouction_exec

